/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.model.service;

import com.model.domain.persona.Hogar;
import com.model.domain.persona.HogarXml;
import com.model.domain.proceso.envio.carga.ConjuntoControlesEnvioHogar;
import com.model.domain.proceso.envio.carga.ControlEnvioHogar;
import com.model.domain.proceso.envio.carga.ReglaEnvioHogar;
import com.model.domain.proceso.envio.carga.LogXmlCargado;
import com.model.domain.proceso.envio.subida.LogXmlTransferido;
import java.sql.Connection;
import java.sql.PreparedStatement;
import util.log.MiLogger;

/**
 * @author Rdelacruz
 */
public class ControlEnvioHogarService 
extends AbstractService
{
    private HogarXmlTransferidoService hogarXmlTransferidoService;
    
    public void save(Connection con, MiLogger logger, LogXmlCargado logXmlCargado) throws Exception {           
        
        PreparedStatement ps_control = null;
        try {
            String sql = "insert into Envios_control_hogar (id_carga, id_envio, hogarxml, resFinCampoHogar, resultado) "
                    + " values(?,?,?,?,?)";
            ps_control = con.prepareStatement(sql); 
            
            
            
            ConjuntoControlesEnvioHogar controles = logXmlCargado.getHogaresParaCargar();
            for(ControlEnvioHogar control : controles.getControlesEnvioHogar()) {
                control.setLogXmlCargado(logXmlCargado);
                
                LogXmlTransferido logxml = logXmlCargado.getLogXmlTransferido();
                ReglaEnvioHogar regla = control.getReglaEnvioHogar();                
                HogarXml hxml = regla.getHogarXml();
                Hogar hbd = hxml.getHogarBD();                
                        
                ps_control.setInt(1, logXmlCargado.getId());
                ps_control.setInt(2, logxml.getId());
                ps_control.setString(3, hxml.getCodigoHogar());
                
                if(hbd==null)
                    ps_control.setString(4, null);
                else
                    ps_control.setString(4, hbd.getResultadoFinalString());
                
                ps_control.setInt(5, control.getEstado().getId());
                
                //logger.logd(control.getMensaje());
                logger.logm(control.getMensaje());
                
                /*
                //Para ver la consulta SQL
                StringBuilder sb = new StringBuilder();
                sb.append("id_carga="+logXmlCargado.getId());
                sb.append(" id_envio="+logXmlCargado.getLogXmlSubido().getId());
                sb.append(" hogarxml="+regla.getHogarXml().getHogar());
                sb.append(" resFinCampoHogar="+regla.getHogarXml().getHogarBD().getResultadoFinal());
                sb.append(" resultado="+regla.puedeCargar());
                System.out.println("SQL= "+sb.toString());
                */                
                ps_control.executeUpdate();
                
                
                //Actualizando hogarXmlTransferido              
                hogarXmlTransferidoService.updateUltimaCarga(con, control);
            }      
        }
        finally {
            if(ps_control!=null) ps_control.close();
        }
    }    

    public HogarXmlTransferidoService getHogarXmlTransferidoService() {
        return hogarXmlTransferidoService;
    }

    public void setHogarXmlTransferidoService(HogarXmlTransferidoService hogarXmlTransferidoService) {
        this.hogarXmlTransferidoService = hogarXmlTransferidoService;
    }
    
    
}